package cn.edu.uestc.framework.ajax;

import java.io.Serializable;

/**
 * 用于返回ajax
 * =====================
 * sc    |    body
 * ---------------------
 * 200     | 请求的数据
 * ---------------------
 * 500     | 系统异常 无
 * ---------------------
 * 其他    | 业务异常 错误信息
 * =====================
 * Created by Eric on 2017/6/12.
 */
public class AjaxResponse implements Serializable {
    public static final String SUCCESS = "success";
    private static final int OK = 200;
    private static final int SERVER_ERROR = 500;
    private int sc;
    private String body;

    /**
     * 用于构建成功的返回
     *
     * @param body 需要返回的内容
     * @return 构建成功的response
     */
    public static AjaxResponse getOKResponse(String body) {
        return new AjaxResponse(AjaxResponse.OK, body);
    }

    /**
     * 用于构建系统错误返回
     *
     * @return 一个SC=500,body=""的错误
     */
    public static AjaxResponse getServerErrorResponse() {
        return new AjaxResponse(AjaxResponse.SERVER_ERROR, "");
    }

    /**
     * 用于获得业务错误
     *
     * @param sc   错误id
     * @param body 错误信息
     * @return 一个封装好的response
     */
    public static AjaxResponse getBusinessErrorResponse(int sc, String body) {
        return new AjaxResponse(sc, body);
    }

    private AjaxResponse(int sc, String body) {
        this.sc = sc;
        this.body = body;
    }

    public int getSc() {
        return sc;
    }

    public void setSc(int sc) {
        this.sc = sc;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
